<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/15
 * Time: 12:04
 */

namespace app\admin\controller;


use app\admin\model\TeamModel;
use cmf\controller\AdminBaseController;
use think\Db;

class TeamController extends AdminBaseController
{
    protected $arrWhere = [];

    /**
     * 团队展示
     */
    public function team_list()
    {
        $username = trim(request()->param('user_name'));
        $email = trim(request()->param('email'));
        //筛选条件
        if(!empty($username)){
            $this->arrWhere['user_name'] = ['like','%'.$username.'%'];
        }
        $teams = Db::name('team')->where($this->arrWhere)->where('status',1)->order('top desc')->paginate(8);
        $teams->appends(['user_name'=>$username,'email'=>$email]);
        //获取分页参数
        $page = $teams->render();

        return view('',['teams'=>$teams,'page'=>$page]);
    }

    /**
     * 置顶
     */
    public function top()
    {
        $id = request()->param('id');
        $top = Db::name('team')->where('status',1)->order('top desc')->limit(1)->value('top');
        $result = Db::name('team')
            ->where('id', $id)
            ->update(['top' => $top+1]);
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }


    /**
     *添加团队成员
     */
    public function add()
    {
        if(request()->isPost()){
            $post = request()->param();
            $post['update_time'] = $post['create_time'];
            $team_model = new TeamModel();
            $result = $team_model->allowField(true)->save($post);
            if($result){
                $this->success('添加成功!', url('team/team_list'));
            }else{
                $this->error('添加失败！',url('team/add'));
            }
        }

        return view();
    }

    /**
     * 团队成员修改
     */
    public function edit()
    {
        $id = request()->param('id');
        $info = Db::name('team')->where('id',$id)->find();
        if(request()->post()){
            $data = request()->param();
            $team_model = new TeamModel();
            $result = $team_model->allowField(true)->isUpdate(true)->data($data, true)->save();
            if($result){
                $this->success('修改成功!', url('team/team_list'));
            }else{
                $this->error('修改失败！',url('team/edit'));
            }
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 成员删除
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (Db::name('team')->delete($id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

}